package controller;

import service.UserService;
import vo.Employee;

import javax.management.remote.rmi._RMIConnection_Stub;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        UserService userService = new UserService();

        HttpSession session = request.getSession();

        Map<String, Object> emp = (Map<String, Object>) session.getAttribute("emp");

        if(null==emp || emp.size()==0){

            String username = request.getParameter("username");
            String password = request.getParameter("password");

            Map<String, Object> loginEmp = userService.login(username, password);

            //登录成功
            if(null!=loginEmp && loginEmp.size()>0){
                System.out.println("登录成功");
                session.setAttribute("emp",loginEmp);
                request.getRequestDispatcher("/jsp/index.jsp").forward(request,response);
            }else{
                System.out.println("登录失败");
                //登录失败，给失败信息
                request.setAttribute("loginInfo","用户名或密码错误");
                request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
            }

        }else{
            request.getRequestDispatcher("/jsp/index.jsp").forward(request,response);
        }



    }
}
